﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmLabourContractInfo : Form
    {
        int _LCID;
        // int _LCIDMax;
        int _EmployeeID;
        bool isChange = false;
        LabourContractInfo _LCInfo;
        DialogResult dlgResult;
        LabourContractClient wcfLC;
        RoleRightInfo listRole;
        EmployeeInfo _EmployeeInfo;
        EmployeeClient _wcfEmployee;
        int _TotalMonth = 0;

        public frmLabourContractInfo(int LCID, int EmployeeID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            LoadLanguage();
            
            //txtLC_Date.Text = DateTime.Now.ToString("dd/MM/yyyy");
            DateTextBox dtb1 = new DateTextBox(txtLC_Date);
            DateTextBox dtb2 = new DateTextBox(txtDateStart);
            DateTextBox dtb3 = new DateTextBox(txtDateEnd);
            DateTextBox dtb4 = new DateTextBox(mtxBeginProbation);
            DateTextBox dtb5 = new DateTextBox(mtxEndProbation);

            UISetting.LoadBranch(ref cboBranch);
            UISetting.LoadPositionByBranchCode(ref cboPost, Program.strBranchCode);
            UISetting.LoadContractTypeByBranchCode(ref cboContract_Type, Program.strBranchCode);
            _LCID = LCID;
            _EmployeeID = EmployeeID;
            dlgResult = DialogResult.Cancel;
            _LCInfo = new LabourContractInfo();
            wcfLC = Service.LabourContract();
            _wcfEmployee = Service.Employee();

            if (_LCID > 0)
            {
                _LCInfo = wcfLC.LabourContract_GetInfo(_LCID);
                Info2Form();
            }
            listRole = _listRole;
            isChange = false;
        }

        public void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel1);
        }

        private void Info2Form()
        {
            txtDateStart.Text = _LCInfo.BeginDate == DateTime.MinValue ? "" : _LCInfo.BeginDate.ToString("dd/MM/yyyy");
            txtDateEnd.Text = _LCInfo.FinishDate == DateTime.MinValue ? "" : _LCInfo.FinishDate.ToString("dd/MM/yyyy");
            mtxBeginProbation.Text = _LCInfo.BeginProbationPeriod == DateTime.MinValue ? "" : _LCInfo.BeginProbationPeriod.ToString("dd/MM/yyyy");
            mtxEndProbation.Text = _LCInfo.EndProbationPeriod == DateTime.MinValue ? "" : _LCInfo.EndProbationPeriod.ToString("dd/MM/yyyy");
            txtWorkPlace.Text = _LCInfo.PlaceOfWork;
            cboBranch.SelectedValue = GetBranh(_LCInfo.DepartmentID);
            cboDept.SelectedValue = _LCInfo.DepartmentID;
            cboPost.SelectedValue = _LCInfo.PositionID;
            txtDes.Text = _LCInfo.Description;
            txtLC_Date.Text = _LCInfo.Date.ToString("dd/MM/yyyy");
            txtLC_No.Text = _LCInfo.No;
            txtLC_Salary.Text = _LCInfo.Salary.ToString("#,##0.0#");
            cboContract_Type.SelectedValue = _LCInfo.ContractTypeID;
            isChange = false;
        }
        int GetBranh(int DepartmentID)
        {
            DepartmentClient ws = Service.Department();
            DepartmentInfo info = new DepartmentInfo();
            info = ws.Department_GetInfo(DepartmentID);
            return  info!=null ? info.BranchID : 0;
        }
        void Form2Info()
        {
            _LCInfo.ID = _LCID;
            _LCInfo.EmployeeID = _EmployeeID;
            _LCInfo.FinishDate = UISetting.GetDate(txtDateEnd.Text);
            _LCInfo.BeginDate = UISetting.GetDate(txtDateStart.Text);
            _LCInfo.BeginProbationPeriod = UISetting.GetDate(mtxBeginProbation.Text);
            _LCInfo.EndProbationPeriod = UISetting.GetDate(mtxEndProbation.Text);
            _LCInfo.PlaceOfWork = txtWorkPlace.Text;
            _LCInfo.PositionID = Convert.ToInt32(cboPost.SelectedValue);
            _LCInfo.DepartmentID = Convert.ToInt32(cboDept.SelectedValue);
            _LCInfo.Description = txtDes.Text.Trim();
            _LCInfo.Date = UISetting.GetDate(txtLC_Date.Text);
            _LCInfo.No = txtLC_No.Text;
            _LCInfo.Salary = Convert.ToDecimal(txtLC_Salary.Text);
            _LCInfo.ContractTypeID = Convert.ToInt32(cboContract_Type.SelectedValue);
            if (_LCInfo.ID == 0)
                _LCInfo.IsActive = true;
            else
                _LCInfo.IsActive = _LCInfo.IsActive;
        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            //bỏ kiểm Tra ngày tháng. tuyvv, 7/2012
            return true;

            if (txtLC_No.Text.Trim().Length == 0)
            {
                txtLC_No.Focus();
                MessageBox.Show(UpdateUI.GetString("sohopdongtrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (!CheckTextValid.IsDateTime(txtLC_Date.Text, "dd/MM/yyyy"))
            {
                txtLC_Date.Focus();
                MessageBox.Show(UpdateUI.GetString("ngaykykhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (Convert.ToInt32(cboContract_Type.SelectedValue) == 0)
            {
                cboContract_Type.Focus();
                MessageBox.Show(UpdateUI.GetString("chuachonloaihopdong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (_EmployeeID > 0)
            {
                _EmployeeInfo = _wcfEmployee.Employee_GetInfo(_EmployeeID);
                if (_EmployeeInfo != null)
                {
                    
                    if (DateTime.Compare(UISetting.GetDate(txtLC_Date.Text), _EmployeeInfo.StartDate) < 0)
                    {
                        // Ngày ký phải lớn hơn ngày vào công ty:
                        MessageBox.Show(UpdateUI.GetString("ngaykyphailonhonngayvaocongty"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return false;
                    }
                }
            }
            if (UISetting.GetDate(txtDateStart.Text) == DateTime.MinValue)
            {
                txtDateStart.Focus();
                MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            else
                if (DateTime.Compare(UISetting.GetDate(txtDateStart.Text), UISetting.GetDate(txtLC_Date.Text)) < 0)
                {
                    // Ngày bắt đầu hiệu lực phải lớn hơn ngày vào công ty:
                    MessageBox.Show("Ngày bắt đầu hiệu lực phải lớn hơn hoặc bằng ngày ký", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }

            if (_TotalMonth != 0)//Kiểm tra nếu không phải hợp đồng không xác định thời hạn:
            {
                if (UISetting.GetDate(txtDateEnd.Text) == DateTime.MinValue)
                {
                    txtDateEnd.Focus();
                    MessageBox.Show(UpdateUI.GetString("ngayketthuckhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
                if (DateTime.Compare(UISetting.GetDate(txtDateEnd.Text), UISetting.GetDate(txtDateStart.Text)) < 0)
                {
                    // Ngày kết thúc phải muộn hơn ngày bắt đầu:
                    MessageBox.Show(UpdateUI.GetString("ngayketthucphaimuonhonngaybatdau"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
            }

            if (mtxBeginProbation.Text.Trim() == "/  /")
                mtxBeginProbation.Text = "";
            else
            {
                if (!CheckTextValid.IsDateTime(mtxBeginProbation.Text, "dd/MM/yyyy"))
                {
                    mtxBeginProbation.Focus();
                    MessageBox.Show(UpdateUI.GetString("ngaybatdauthuvieckhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
            }
            if (mtxEndProbation.Text.Trim() == "/  /")
                mtxEndProbation.Text = "";
            else
            {
                if (!CheckTextValid.IsDateTime(mtxEndProbation.Text, "dd/MM/yyyy"))
                {
                    mtxEndProbation.Focus();
                    MessageBox.Show(UpdateUI.GetString("ngayketthucthucvieckhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
                if (DateTime.Compare(UISetting.GetDate(mtxEndProbation.Text), UISetting.GetDate(mtxBeginProbation.Text)) < 0)
                {
                    // Ngày kết thúc phải lớn hơn ngày bắt đầu:
                    MessageBox.Show(UpdateUI.GetString("ngayketthucphaimuonhonngaybatdau"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
            }
            if (Convert.ToInt32(cboPost.SelectedValue) == 0)
            {
                cboPost.Focus();
                MessageBox.Show(UpdateUI.GetString("chuachonchucdanhchuyenmon"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (Convert.ToInt32(cboDept.SelectedValue) == 0)
            {
                cboDept.Focus();
                MessageBox.Show(UpdateUI.GetString("chuachonphongban"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (_LCInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _LCID = 0;
            _LCInfo = new LabourContractInfo();
            txtDateEnd.Text = "";
            txtDateStart.Text = "";
            txtDes.Text = "";
            txtLC_Date.Text = "";
            txtLC_No.Text = "";
            cboContract_Type.SelectedValue = 0;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            Form2Info();
            if (_LCID > 0)//update
            {
                if (!listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (wcfLC.LabourContract_Update(_LCInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                _LCInfo.ID = wcfLC.LabourContract_Insert(_LCInfo);
                _LCID = _LCInfo.ID;
                if (_LCID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("taomoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmLabourContractInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }

        }

        private void frmLabourContractInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            _wcfEmployee.Close();
            wcfLC.Close();
            this.DialogResult = dlgResult;

        }

        private void txtDateStart_Leave(object sender, EventArgs e)
        {
            //DateTime _dateVal;
            //if (!DateTime.TryParseExact(txtDateStart.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out _dateVal))
            //{
            //    txtDateStart.Text = "";
            //    txtDateStart.Focus();

            //    MessageBox.Show(UpdateUI.GetString("nhapsaingaythang"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //}
            //else
            //{
            //    ContractTypeClient wcfContract = Service.ContractType();
            //    _ContractInfo = new ContractTypeInfo();
            //    _ContractInfo = wcfContract.ContractType_GetInfo(Convert.ToInt32(cboContract_Type.SelectedValue));
            //    int _Time = _ContractInfo.Time;
            //    DateTime _dateBegin = DateTime.ParseExact(txtDateStart.Text, "dd/MM/yyyy", null);
            //    wcfContract.Close();
            //    if (_Time > 0)
            //    {
            //        DateTime _dateEnd = _dateBegin.AddMonths(_Time);
            //        _dateEnd = _dateEnd.AddDays(-1);
            //        txtDateEnd.Text = _dateEnd.ToString("dd/MM/yyyy");
            //    }
            //    else
            //        txtDateEnd.Text = "";
            //}
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (!listRole.Print)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenin"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (_LCID > 0)
            {
                frmPreviewReport f;
                //if (mtxEndProbation.Text != "/  /")
                //{
                //    if (DateTime.Compare(UISetting.GetDate(mtxEndProbation.Text), DateTime.Now.Date) > 0)
                //        f = new frmPreviewReport("LabourProbationContract", new string[] { _LCID.ToString() });
                //    else
                //        f = new frmPreviewReport("LabourContract", new string[] { _LCID.ToString() });
                //    f.ShowDialog();
                //}
                //else
                {
                    f = new frmPreviewReport("LabourContract", new string[] { _LCID.ToString() });
                    f.ShowDialog();
                }
            }
        }

        private void cboContract_Type_SelectedIndexChanged(object sender, EventArgs e)
        {
            ContractTypeClient wcfType = Service.ContractType();
            ContractTypeInfo _Info = new ContractTypeInfo();
            int _ID = 0;
            if (cboContract_Type.SelectedValue != null)
            {
                _ID = Convert.ToInt32(cboContract_Type.SelectedValue);
                _Info = wcfType.ContractType_GetInfo(_ID);
                if (_Info != null)
                    _TotalMonth = _Info.Time;
            }
        }

        private void cboBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            UISetting.LoadDepartment(ref cboDept, Convert.ToInt32(cboBranch.SelectedValue));
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }

        private void btnLCList_Click(object sender, EventArgs e)
        {
            if (_LCID > 0)
            {
                frmPreviewReport f = new frmPreviewReport("LabourContractMenu", new string[] { _LCID.ToString() });
                f.ShowDialog();
            }
        }

        private void DataChanged(object sender, EventArgs e)
        {
            isChange = true;
        }

    }
}
